package org.jeecg.qis.controller;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.basic.entity.MesBomHeaderSetup;
import org.jeecg.basic.vo.MesOptionVO;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.prod.entity.MesBomHeaderWo;
import org.jeecg.prod.entity.MesWoSrc;
import org.jeecg.qis.entity.QisProcdetailValue;
import org.jeecg.qis.entity.QisProcheadValue;
import org.jeecg.qis.service.IQisProcdetailValueService;
import org.jeecg.qis.service.IQisProcheadValueService;
import org.jeecg.common.api.vo.Result;
import org.jeecg.qis.entity.QisProcrecord;
import org.jeecg.qis.service.IQisProcrecordService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.qis.vo.QisProcDetailValueVO;
import org.jeecg.qis.vo.QisProcInspectHisVO;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: qis_procrecord
 * @Author: licy
 * @Date: 2023-05-21
 * @Version: V1.0
 */
@Api(tags = "qis_procrecord")
@RestController
@RequestMapping("/qis/qisProcrecord")
@Slf4j
public class QisProcrecordController extends JeecgController<QisProcrecord, IQisProcrecordService> {
	@Autowired
	private IQisProcrecordService qisProcrecordService;
	@Autowired
	private IQisProcheadValueService iQisProcheadValueService;
	@Autowired
	private IQisProcdetailValueService iQisProcdetailValueService;

	/**
	 * 单表分页列表查询
	 *
	 * @param qisProcrecord
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value = "qis_procrecord-分页列表查询", notes = "qis_procrecord-分页列表查询")
	@GetMapping(value = "/list")
	@DS("#header.his")
	public Result<IPage<QisProcrecord>> queryPageList(QisProcrecord qisProcrecord,
													  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
													  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
													  HttpServletRequest req) {
		QueryWrapper<QisProcrecord> queryWrapper = QueryGenerator.initQueryWrapper(qisProcrecord, req.getParameterMap());
		queryWrapper.orderByDesc("CTIME");
		Page<QisProcrecord> page = new Page<QisProcrecord>(pageNo, pageSize);
		IPage<QisProcrecord> pageList = qisProcrecordService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 * 单表分页列表查询
	 *
	 * @param qisProcrecord
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value = "qis_procrecord-分页列表查询", notes = "qis_procrecord-分页列表查询")
	@GetMapping(value = "/opList")
	@DS("#header.his")
	public Result<List<Map<String, String>>> queryOpList(QisProcrecord qisProcrecord,
														 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
														 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
														 HttpServletRequest req) {
		List<Map<String, String>> queryOpList = iQisProcheadValueService.queryOpList(qisProcrecord);
		return Result.OK(queryOpList);
	}

	/**
	 * 单表分页列表查询
	 *
	 * @param param
	 * @return
	 */
	@ApiOperation(value = "qis_procrecord-分页列表查询", notes = "qis_procrecord-分页列表查询")
	@GetMapping(value = "/detailColumnList")
	@DS("#header.his")
	public Result<List<Map<String, String>>> queryDetailColumnList(Map<String, String> param) {
		List<Map<String, String>> data = iQisProcdetailValueService.queryDetailColumnList(param);
		return Result.OK(data);
	}

	/**
	 * 单表分页列表查询
	 *
	 * @param param
	 * @return
	 */
	@ApiOperation(value = "qis_procrecord-分页列表查询", notes = "qis_procrecord-分页列表查询")
	@GetMapping(value = "/detailList")
	@DS("#header.his")
	public Result<List<Map<String, Object>>> queryDetailList(Map<String, String> param) {
		List<Map<String, Object>> maps = iQisProcdetailValueService.queryDetailList(param);
		return Result.OK(maps);
	}

	/**
	 * 添加
	 *
	 * @param qisProcrecord
	 * @return
	 */
	@AutoLog(value = "qis_procrecord-添加")
	@ApiOperation(value = "qis_procrecord-添加", notes = "qis_procrecord-添加")
	@PostMapping(value = "/addRecord")
	@DS("#header.line")
	public Result<String> add(@RequestBody QisProcrecord qisProcrecord) {
		return qisProcrecordService.addRecord(qisProcrecord);
	}


	/**
	 * 添加
	 *
	 * @param woid
	 * @param type
	 * @return
	 */
	@ApiOperation(value = "根据工单号获取工序列表", notes = "根据工单号获取工序列表")
	@GetMapping(value = "/queryOPList")
	@DS("#header.his")
	public Result<List<MesOptionVO>> queryOPList(String woid,String type) {
		return qisProcrecordService.queryOPListByWoid(woid,type);
	}

	/**
	 * 查询检验单子工序检验单
	 *
	 * @param checkNo
	 * @return
	 */
	@ApiOperation(value = "根据工单号获取工序列表", notes = "根据工单号获取工序列表")
	@GetMapping(value = "/initLeft")
	@DS("#header.his")
	public Result<IPage<QisProcheadValue>> initLeft(String checkNo) {
		QueryWrapper<QisProcheadValue> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("CHECKLISTNO", checkNo);
		queryWrapper.orderByAsc("OP");
		Page<QisProcheadValue> page = new Page<QisProcheadValue>(1, 100);
		//IPage<QisProcrecord> pageList = qisProcrecordService.page(page, queryWrapper);
		IPage<QisProcheadValue> list = iQisProcheadValueService.page(page,queryWrapper);
		return Result.OK(list);
	}

	/**
	 * 查询检验单子工序项目列表
	 *
	 * @param recId
	 * @return
	 */
	@ApiOperation(value = "根据工单号获取工序列表", notes = "根据工单号获取工序列表")
	@GetMapping(value = "/initRight")
	@DS("#header.his")
	public Result<List<QisProcDetailValueVO>> initRight(String recId) {
		List<QisProcDetailValueVO> list =iQisProcdetailValueService.initRight(recId);
		return Result.OK(list);
	}

	/**
	 * 查询检验单子工序项目列表
	 *
	 * @param recId
	 * @return
	 */
	@ApiOperation(value = "根据项目号和检验单号获取检验列表", notes = "根据工单号获取工序列表")
	@GetMapping(value = "/initDetails")
	@DS("#header.his")
	public Result<List<QisProcdetailValue>> initDetails(String recId,String rid) {
		QueryWrapper<QisProcdetailValue> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("DATAID", recId).eq("HEADID", rid);
		List<QisProcdetailValue> list = iQisProcdetailValueService.list(queryWrapper);
		return Result.OK(list);
	}

	/**
	 * 查询检验单子工序项目列表
	 *
	 * @param list
	 * @return
	 */
	@ApiOperation(value = "批量录入检验单内容", notes = "批量录入检验单内容")
	@PostMapping(value = "/saveDetails")
	@DS("#header.line")
	public Result<Boolean> saveDetails(@RequestBody List<QisProcdetailValue> list) {
		list.forEach(item->{
			iQisProcdetailValueService.updateById(item);
		});
		List<String> uniqueHeadIds = list.stream()
				.map(QisProcdetailValue::getHeadid) // 提取 headid 属性
				.distinct() // 去重
				.collect(Collectors.toList()); // 收集为新的列表
		//根据录入的结果判断该工序是否整体合格
		iQisProcheadValueService.saveHeadsById(uniqueHeadIds);
		//判断整张单子是否合格，更新检验单据状态与结果
		qisProcrecordService.updateRecordStatus(uniqueHeadIds.get(0));
		return Result.OK("录入成功！");
	}

	/**
	 * 添加
	 *
	 * @param qisProcrecord
	 * @return
	 */
	@AutoLog(value = "新增过程检验单")
	@ApiOperation(value = "qis_procrecord-添加", notes = "qis_procrecord-添加")
	@PostMapping(value = "/createRecord")
	@DS("#header.line")
	public Result<String> createRecord(@RequestBody QisProcrecord qisProcrecord) {
		return qisProcrecordService.createRecord(qisProcrecord);
	}


	/**
	 * 添加
	 *
	 * @return
	 */
	@ApiOperation(value = "getWoOption-添加", notes = "getWoOption-添加")
	@GetMapping(value = "/getWoOption")
	@DS("#header.his")
	public Result<List<MesBomHeaderWo>> getWoOption() {
		return qisProcrecordService.getWoOption();
	}





	/**
	 * 通过map删除
	 *
	 * @param map
	 * @return
	 */
	@AutoLog(value = "qis_procrecord-通过id删除")
	@ApiOperation(value = "qis_procrecord-通过id删除", notes = "qis_procrecord-通过id删除")
	@RequestMapping(value = "/delete")
	@DS("#header.line")
	public Result<String> delete(@RequestBody QisProcrecord qisProcrecord) {
		qisProcrecordService.delMain(qisProcrecord.getChecklistno());
		return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "qis_procrecord-批量删除")
	@ApiOperation(value = "qis_procrecord-批量删除", notes = "qis_procrecord-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	@DS("#header.line")
	public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
		this.qisProcrecordService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "qis_procrecord-通过id查询")
	@ApiOperation(value = "qis_procrecord-通过id查询", notes = "qis_procrecord-通过id查询")
	@GetMapping(value = "/queryById")
	@DS("#header.his")
	public Result<QisProcrecord> queryById(@RequestParam(name = "id", required = true) String id) {
		QisProcrecord qisProcrecord = qisProcrecordService.getById(id);
		if (qisProcrecord == null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(qisProcrecord);
	}



	/**
	 * 导出excel
	 *
	 * @param request
	 * @param param
	 */
	@RequestMapping(value = "/exportXls")
	@DS("#header.his")
	public ModelAndView exportXls(HttpServletRequest request, @RequestParam Map<String, String> param) {
		// Step.1 组装查询条件
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		Page<QisProcInspectHisVO> page = new Page<QisProcInspectHisVO>(1, 20000);
		IPage<QisProcInspectHisVO> pageList = iQisProcdetailValueService.getProcInspectHispage(page, param);
		// Step.2 获取导出数据

		// Step.3 AutoPoi 导出Excel
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		//此处设置的filename无效 ,前端会重更新设置一下
		mv.addObject(NormalExcelConstants.FILE_NAME, "过程检验追溯");
		mv.addObject(NormalExcelConstants.CLASS, QisProcInspectHisVO.class);
		//update-begin--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置--------------------
		ExportParams exportParams = new ExportParams("过程检验追溯报表", "导出人:" + sysUser.getRealname(), "过程检验追溯");
		//update-end--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置----------------------
		mv.addObject(NormalExcelConstants.PARAMS, exportParams);
		mv.addObject(NormalExcelConstants.DATA_LIST, pageList.getRecords());
		return mv;
	}

	/**
	 * 通过excel导入数据
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	@DS("#header.line")
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, QisProcrecord.class);
	}


	/**
	 * 单表分页列表查询
	 *
	 * @param param
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value = "过程检验历史追溯列表", notes = "过程检验历史追溯列表")
	@GetMapping(value = "/queryPageProcHisList")
	@DS("#header.his")
	public Result<IPage<QisProcInspectHisVO>> queryPageProcHisList(@RequestParam Map<String, String> param,
																   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
																   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
																   HttpServletRequest req) {
		Page<QisProcInspectHisVO> page = new Page<QisProcInspectHisVO>(pageNo, pageSize);
		IPage<QisProcInspectHisVO> pageList = iQisProcdetailValueService.getProcInspectHispage(page, param);
		return Result.OK(pageList);
	}
}
